package com.lean.android.ad.utils

import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Locale

const val ONE_SECOND: Long = 1000
const val ONE_MINUTE = 60 * ONE_SECOND
const val ONE_HOUR = 60 * ONE_MINUTE
const val ONE_DAY = 24 * ONE_HOUR
const val ONE_WEEK = 7 * ONE_DAY
const val ONE_MONTH = 30 * ONE_DAY

private val dateFormat by lazy {
    SimpleDateFormat("yyyy-MM-dd", Locale.CHINA)
}

fun tomorrowTime(): Long{
    val calendar = Calendar.getInstance()
    calendar.timeInMillis = System.currentTimeMillis()
    calendar.add(Calendar.DATE, 1)
    return calendar.timeInMillis
}

fun yesterdayTime(): Long{
    val calendar = Calendar.getInstance()
    calendar.timeInMillis = System.currentTimeMillis()
    calendar.add(Calendar.DATE, -1)
    return calendar.timeInMillis
}

fun isSameDay(timestamp: Long): Boolean {
    val calendar = Calendar.getInstance()
    calendar.timeInMillis = timestamp
    val year = calendar.get(Calendar.YEAR)
    val daysOfYear = calendar.get(Calendar.DAY_OF_YEAR)

    calendar.timeInMillis = System.currentTimeMillis()
    val currentYear = calendar.get(Calendar.YEAR)
    val currentDaysOfYear = calendar.get(Calendar.DAY_OF_YEAR)

    return year == currentYear && daysOfYear == currentDaysOfYear
}


private fun isYesterday(timestamp: Long): Boolean {
    val calendar = Calendar.getInstance()
    calendar.timeInMillis = timestamp
    val year = calendar.get(Calendar.YEAR)
    val daysOfYear = calendar.get(Calendar.DAY_OF_YEAR)

    calendar.timeInMillis = System.currentTimeMillis()
    val currentYear = calendar.get(Calendar.YEAR)
    val currentDaysOfYear = calendar.get(Calendar.DAY_OF_YEAR)

    return year == currentYear && currentDaysOfYear - daysOfYear == 1
}

private fun isSameWeek(timestamp: Long): Boolean {
    val calendar = Calendar.getInstance()
    calendar.timeInMillis = timestamp
    val year = calendar.get(Calendar.YEAR)
    val daysOfYear = calendar.get(Calendar.DAY_OF_YEAR)


    calendar.timeInMillis = System.currentTimeMillis()
    val currentYear = calendar.get(Calendar.YEAR)
    val currentDaysOfYear = calendar.get(Calendar.DAY_OF_YEAR)

    return currentYear == year && currentDaysOfYear - daysOfYear < 7

}

fun formatWeek(timestamp: Long): String {
    val calendar = Calendar.getInstance()
    calendar.timeInMillis = timestamp
    return when (calendar.get(Calendar.DAY_OF_WEEK)) {
        Calendar.SUNDAY -> "星期日"
        Calendar.MONDAY -> "星期一"
        Calendar.TUESDAY -> "星期二"
        Calendar.WEDNESDAY -> "星期三"
        Calendar.THURSDAY -> "星期四"
        Calendar.FRIDAY -> "星期五"
        else -> "星期六"
    }
}

fun parseDate(dateStr: String): Long {
    return try {
        dateFormat.parse(dateStr)?.time ?: 0
    } catch (e: Exception) {
        0L
    }
}

private fun formatSeconds(mills: Long): Long {
    return mills / ONE_SECOND
}

private fun formatMinutes(mills: Long): Long {
    return mills / ONE_MINUTE
}

private fun formatHours(mills: Long): Long {
    return mills / ONE_HOUR
}

private fun formatDays(mills: Long): Long {
    return mills / ONE_DAY
}

private fun isSameYear(timestamp: Long): Boolean {
    val calendar = Calendar.getInstance()
    calendar.timeInMillis = timestamp
    val year = calendar.get(Calendar.YEAR)

    calendar.timeInMillis = System.currentTimeMillis()
    val currentYear = calendar.get(Calendar.YEAR)

    return year == currentYear
}
